<!--
  * 任务列表
-->
<template>
  <a-form class="smart-query-form" v-privilege="'task:query'">
    <a-row class="smart-query-form-row">
      <a-form-item label="实施人员" class="smart-query-form-item" name="implementer">
        <HasUsersSelect v-model:value="queryForm.userId" style="width: 200px" :showSearch="true" :allowClear="true" placeholder="实施人员" />
      </a-form-item>

      <a-form-item label="周报周期" name="weeklyStart" class="smart-query-form-item">
        <a-week-picker v-model:value="queryForm.weeklyDate" :defaultPickerValue="defaultTime" style="width: 200px" />
      </a-form-item>
      <a-form-item class="smart-query-form-item smart-margin-left10">
        <a-button type="primary" @click="ajaxQuery">
          <template #icon>
            <SearchOutlined />
          </template>
          查询
        </a-button>
        <a-button @click="resetQuery">
          <template #icon>
            <ReloadOutlined />
          </template>
          重置
        </a-button>
      </a-form-item>
    </a-row>
  </a-form>

  <a-card size="small" :bordered="false" :hoverable="true">
    <a-table
      :scroll="{ x: 1200 }"
      size="small"
      :dataSource="tableData"
      :columns="columns"
      rowKey="id"
      :pagination="false"
      :loading="tableLoading"
      bordered
    >
      <template #expandedRowRender="{ record }">
        <a-table
          :columns="detailColumns"
          :data-source="record.datailList"
          :pagination="false"
        >
          <template #bodyCell="{ column, datail, text }">
          <template v-if="column.dataIndex === 'presentRate'">
            <span>{{ text }}%</span>
          </template>
          </template>
        </a-table>
      </template>
      <template #bodyCell="{ column, record, text }">
        <template v-if="column.dataIndex === 'taskRate'">
          <span>{{ text }}%</span>
        </template>
        <template v-if="column.dataIndex === 'action'">
          <div class="smart-table-operate">
            <a-button v-if=" record.stateName==='进行中' " @click="confirmComplete(record.id)" type="link">完成任务</a-button>
          </div>
        </template>
      </template>
    </a-table>
  </a-card>
</template>
<script setup lang="ts">
  import { reactive, ref, onMounted,computed} from 'vue';
  import { taskApi } from '/@/api/task/task-api';
  import { smartSentry } from '/@/lib/smart-sentry';
  import { useUserStore } from '/@/store/modules/system/user';
  import dayjs, { Dayjs } from 'dayjs';
  import { message,Modal } from 'ant-design-vue';
  import HasUsersSelect from '/@/components/business/task/has-users-select.vue';
  // --------------------------- 企业表格 列 ---------------------------

  const columns = ref([
  {
      title: '操作',
      dataIndex: 'action',
      width: 80,
      ellipsis: true,
    },
    {
      title: '任务名称',
      dataIndex: 'name',
      //ellipsis: true,
    },
    {
      title: '上级任务名称',
      dataIndex: 'parentName',
      ellipsis: true,
    },
    {
      title: '负责人',
      dataIndex: 'implementerName',
       width: 80,
    },
    {
      title: '任务进度',
      dataIndex: 'taskRate',
       width: 80,
    },
    {
      title: '计划开始',
       width: 100,
      dataIndex: 'planBegin',
      ellipsis: true,
    },
    {
      title: '计划结束',
       width: 100,
      dataIndex: 'planEnd',
      ellipsis: true,
    },
    {
      title: '任务状态',
       width: 100,
      dataIndex: 'stateName',
      ellipsis: true,
    },
    {
      title: '任务描述',
      dataIndex: 'remark',
      ellipsis: true,
    },
  ]);

  const detailColumns = ref([
    {
      title: '处理日期',
      dataIndex: 'handleDate',
      width: 80,
      ellipsis: true,
    },
    {
      title: '工作内容',
      dataIndex: 'content',
      width: 280,
      //ellipsis: true,
    },
    {
      title: '任务进度',
      dataIndex: 'presentRate',
      width: 80,
      ellipsis: true,
    },
    {
      title: '所用工时',
      dataIndex: 'manHours',
      width: 80,
      ellipsis: true,
    },
    {
      title: '任务风险',
      dataIndex: 'remark',
      width: 280,
      ellipsis: true,
    },
  ]);

  // --------------------------- 查询 ---------------------------
  // let userid = computed(() => useUserStore().employeeId);//获取当前登录用户id
  let userid =  useUserStore().employeeId;//获取当前登录用户id
  const defaultTime = ref<Dayjs>(dayjs().startOf('week'))// 获取本周第一天并格式化
  const queryFormState = {
    userId: userid,
    weeklyDate: defaultTime,
  };
  const queryForm = reactive({ ...queryFormState });
  const tableLoading = ref(false);
  const tableData = ref([]);

  function resetQuery() {
    Object.assign(queryForm, queryFormState);
    ajaxQuery();
  }

   
  function confirmComplete(taskId){
    Modal.confirm({
      title: '确定要结束任务吗？',
      content: '结束任务后，将不能继续填写日报',
      okText: '结束任务',
      okType: 'danger',
      onOk() {
        doCompleteTask(taskId);
      },
      cancelText: '取消',
      onCancel() {},
    });
  }

  async function doCompleteTask(taskId){
    let result = await taskApi.doCompleteTask(taskId);
        message.success(result.message);
        ajaxQuery();
  }

  async function ajaxQuery() {
    try {
      tableLoading.value = true;
      let responseModel = await taskApi.getWeeklyData(queryForm);
      const list = responseModel.data;
      tableData.value = list;
    } catch (e) {
      smartSentry.captureError(e);
    } finally {
      tableLoading.value = false;
    }
  }

  onMounted(ajaxQuery);
</script>
